package com.su.gulimall.gateway.cors;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

@Configuration
//网关的跨域配置类
public class GuliMallCorsConfiguration {
    //跨域拦截器
    @Bean
    public CorsWebFilter corsWebFilter(){
        //该配置类涉及三个对象
        //第一个类：基于url的跨域请求拦截；
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        //对拦截的内容进行配置
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        //配置跨域,所有的请求头，请求方法，请求来源，cookie信息
        corsConfiguration.addAllowedHeader("*");
        corsConfiguration.addAllowedMethod("*");
        corsConfiguration.addAllowedOrigin("*");
        corsConfiguration.setAllowCredentials(true);
        source.registerCorsConfiguration("/**",corsConfiguration);
        return new CorsWebFilter(source);
    }
}
